import GameHandler, { BufType } from "../GameHandler";

export enum StrickCellState{
    MotionLess,
    OnStone,
    Handing,
    Used,
    Discard,
}

export class StrickCell  extends Laya.Script{
    public target:Laya.MeshSprite3D;
    private collider:Laya.PhysicsCollider;

    private duration:number;
    private tween: Laya.Tween;
    private scale:Laya.Vector3;
    private IsDoScale:boolean;

    private IsDoPusDown:boolean;
    private putdownTween:Laya.Tween;
    private pos:Laya.Vector3;
    private rot:Laya.Quaternion;

    private lastRot:Laya.Quaternion;

    private TranckTrans:Laya.Transform3D;
    private IsDoTakeUp:boolean;
    private preTakeupPos:Laya.Vector3;
    private preTakeupRot:Laya.Quaternion;
    private TakeupDuration:number;
    private BeginTakeupTime:number;
    private TakeupFinishHander:any;
    private TakeupFinishFunc:Function;

    private IsRefreshing:boolean;

    private State:StrickCellState;

    constructor(){
        super();
        
        this.duration = 1;
        this.IsDoPusDown = false;
        this.IsDoScale = false;
        this.IsDoTakeUp = false;
        this.TakeupDuration =150;
        this.State = StrickCellState.OnStone;
    }

    onAwake(){
        this.target=this.owner as Laya.MeshSprite3D;
        this.collider = this.owner.getComponent(Laya.PhysicsCollider);
    }
    onEnable(){
        this.RegistEvent();
    }
    onDisable(){
        this.UnRegistEvent();
    }
    private RegistEvent(){
        Laya.stage.on("BrickConflict",this, this.OnBrickConfilick);
    }
    private UnRegistEvent(){
        Laya.stage.off("BrickConflict",this, this.OnBrickConfilick);
    }
    private OnBrickConfilick(strick:Laya.Sprite3D){
        if(strick != this.target){
            return;
        }
        if(this.State == StrickCellState.MotionLess){
            return;
        }
        if(this.State == StrickCellState.Used){
            this.EnableCollider(false);
            this.State = StrickCellState.Discard;
            Laya.timer.once(2000,this,()=>{
                this.target.destroy();
            })
        }
    }

    public SetState(state:StrickCellState){
        this.State = state;
    }

    public get Rotation(){
        return this.target.transform.rotation;
    }
    public get LastRotation(){
        return this.lastRot;
    }
    public SetParent(parent:Laya.Sprite3D){
        parent.addChild(this.target);
    }
    public Set(parent:Laya.Sprite3D,pos:Laya.Vector3,rot:Laya.Quaternion,scale:Laya.Vector3){
        parent.addChild(this.target);
        this.target.transform.position = pos;
        this.target.transform.rotation = rot;
        this.target.transform.localScale = scale;
        this.rot = rot;
        this.pos = pos;
        this.scale = scale;
    }
    public SetParentAndPos(parent:Laya.Sprite3D,pos:Laya.Vector3){
        parent.addChild(this.target);
        this.target.transform.position = pos;
    }
    public SetRotation(rot:Laya.Quaternion){
        this.lastRot = this.target.transform.rotation.clone();
        this.rot = rot;
        this.target.transform.rotation = rot;
    }

    public Rotate(angle:Laya.Vector3){
        this.target.transform.rotate(angle);
    }
    public EnableCollider(enabled:boolean)
    {
        if(this.collider != null){
            this.collider.enabled = enabled;
        }
    }
    public ReduceHigh(high:number){
        this.target.transform.translate(new Laya.Vector3(0,high,0));
    }
    public DoScale(){
        this.IsDoScale = true;
        //this.tween && this.tween.clear();
        this.scale = new Laya.Vector3(0.2,GameHandler.HandStrickScale.y,0.2);
        this.tween = Laya.Tween.to(this.scale,{x:GameHandler.HandStrickScale.x,y:GameHandler.HandStrickScale.y,z:GameHandler.HandStrickScale.z},500,Laya.Ease.bounceOut,new Laya.Handler(this,()=>{this.IsDoScale= false;}));
    }
    public onUpdate(){
        if(this.State == StrickCellState.MotionLess){
            return;
        }
        else if(this.State == StrickCellState.OnStone){
            if(GameHandler.PalyerBuf[BufType.Magnet] == true){
                this.CheckCollection();
            }
        }
        else if(this.State == StrickCellState.Discard){
            this.target.transform.translate(new Laya.Vector3(0,-0.02,0));
        }
        else{
            if(this.IsDoScale){
                this.target.transform.localScale = this.scale;
                }
            if(this.IsDoPusDown){
                this.target.transform.position = this.pos;
                this.target.transform.rotation = this.rot;
                this.target.transform.localScale = this.scale;
            }
            else if(this.IsDoTakeUp){
                let passtime = Laya.systemTimer.currTimer - this.BeginTakeupTime;
                if(passtime < this.TakeupDuration){
                    let lerpvalue = passtime / this.TakeupDuration;
                    Laya.Vector3.lerp(this.preTakeupPos,this.TranckTrans.position,lerpvalue,this.pos);
                    Laya.Quaternion.lerp(this.preTakeupRot,this.TranckTrans.rotation,lerpvalue,this.rot);
                    this.target.transform.position = this.pos;
                    this.target.transform.rotation = this.rot;
                }
                else{
                    this.target.transform.position = this.TranckTrans.position;
                    this.target.transform.rotation = this.TranckTrans.rotation;
                    this.IsDoTakeUp = false;
                    this.TakeupFinishFunc(this.TakeupFinishHander);
                }
                this.target.transform.localScale = this.scale;
            }
            else if(this.IsRefreshing){
                this.target.transform.localScale = this.scale;
                this.target.transform.rotation = this.rot;
            }
        }
    }

    private CheckCollection(){
        if( Math.abs(this.target.transform.position.x-GameHandler.Player.transform.position.x) > GameHandler.MagnetCollectDistance ||
        Math.abs(this.target.transform.position.x-GameHandler.Player.transform.position.x) > GameHandler.MagnetCollectDistance)
        {
            return;
        }
        if(Laya.Vector3.distance(this.target.transform.position,GameHandler.Player.transform.position) < GameHandler.MagnetCollectDistance){
            Laya.stage.event("StrickCollect",this.target);
        }
    }

    public DoPutDown(pos:Laya.Vector3,rot:Laya.Quaternion,scale:Laya.Vector3){
        this.State = StrickCellState.Used;
        this.pos = pos.clone();
        this.pos = this.target.transform.position.clone();
        this.rot = this.target.transform.rotation.clone();
        this.scale = this.target.transform.localScale.clone();
        this.IsDoPusDown = true;

        Laya.Tween.to(this.pos,{x:pos.x,y:pos.y,z:pos.z},500,Laya.Ease.bounceOut,new Laya.Handler(this,()=>{this.IsDoPusDown= false;}));
        Laya.Tween.to(this.rot,{x:rot.x,y:rot.y,z:rot.z,w:rot.w},500,Laya.Ease.bounceOut,new Laya.Handler(this,()=>{this.IsDoPusDown= false;}));
        Laya.Tween.to(this.scale,{x:scale.x,y:scale.y,z:scale.z},500,Laya.Ease.bounceOut,new Laya.Handler(this,()=>{this.IsDoPusDown= false;}));
    }
    public DoFallDown(pos:Laya.Vector3,rot:Laya.Quaternion,scale:Laya.Vector3,time:number){
        this.State = StrickCellState.Used;
        this.pos = pos.clone();
        this.pos = this.target.transform.position.clone();
        this.rot = this.target.transform.rotation.clone();
        this.scale = this.target.transform.localScale.clone();
        this.IsDoPusDown = true;

        Laya.Tween.to(this.pos,{x:pos.x,y:pos.y,z:pos.z},time,Laya.Ease.bounceOut,new Laya.Handler(this,()=>{this.IsDoPusDown= false;}));
        Laya.Tween.to(this.rot,{x:rot.x,y:rot.y,z:rot.z,w:rot.w},time,Laya.Ease.bounceOut,new Laya.Handler(this,()=>{this.IsDoPusDown= false;}));
        Laya.Tween.to(this.scale,{x:scale.x,y:scale.y,z:scale.z},time,Laya.Ease.bounceOut,new Laya.Handler(this,()=>{this.IsDoPusDown= false;}));
    }
    public TackUp(tracktrans:Laya.Transform3D,scale:Laya.Vector3,caller:any,method:Function){
        this.State = StrickCellState.Handing;
        this.TranckTrans = tracktrans;
        this.preTakeupPos = this.target.transform.position;
        this.preTakeupRot = this.target.transform.rotation;
        this.scale = this.target.transform.localScale;
        this.pos = tracktrans.position;
        this.rot = tracktrans.rotation;
        Laya.Tween.to(this.scale,{x:scale.x,y:scale.y,z:scale.z},140,Laya.Ease.linearInOut);
        this.BeginTakeupTime = Laya.systemTimer.currTimer;
        this.TakeupFinishHander = caller;
        this.TakeupFinishFunc = method;
        this.IsDoTakeUp = true;
    }
    public Refrush(rot:Laya.Quaternion,color:Laya.Color){
        this.scale = new Laya.Vector3(0.1,0,0.1) as Laya.Vector3;
        Laya.Tween.to(this.scale,{x:GameHandler.HandStrickScale.x,y:GameHandler.HandStrickScale.y,z:GameHandler.HandStrickScale.z},500,Laya.Ease.bounceOut,new Laya.Handler(this,()=>{this.IsRefreshing= false;}));
        this.rot = this.target.transform.rotation;
        Laya.Tween.to(this.rot,{x:rot.x,y:rot.y,z:rot.z,w:rot.w},100,Laya.Ease.bounceOut);
        let material:Laya.BlinnPhongMaterial  = this.target.meshRenderer.material as Laya.BlinnPhongMaterial;
        material.albedoColor = new Laya.Vector4(color.r,color.g,color.b,1);
        this.IsRefreshing = true;
    }
}